<template>
  <div class="login_container">
    <div class="login-logo">
      公共卫生服务系统平台
    </div>
    <div class="login_box">
      <div class="login-left">
        <div class="el-image">
          <img src="../assets/images/login_banner.png" alt="" />
        </div>
      </div>
      <div class="login-right">
        <div class="login-main">
          <el-form
            ref="loginFormRef"
            :model="loginForm"
            :rules="loginFormRules"
            label-width="0px"
            class="login_form"
          >
            <el-form-item prop="username">
              <el-input
                v-model="loginForm.username"
                prefix-icon="iconfont icon-user"
                clearable
              />
            </el-form-item>

            <el-form-item prop="password">
              <!-- <el-input v-model="loginForm.password" type="password" prefix-icon="iconfont icon-lock_fill"></el-input> -->
              <el-input
                v-model="loginForm.password"
                show-password
                prefix-icon="iconfont icon-lock_fill"
                clearable
              />
            </el-form-item>

            <el-form-item>
              <el-checkbox-group v-model="loginForm.remember">
                <el-checkbox label="记住密码" name="remember" />
              </el-checkbox-group>
            </el-form-item>

            <el-form-item class="btns">
              <el-button type="primary" plain :loading="loading" @click="login"
                >登录</el-button
              >
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      // 表单数据绑定
      loginForm: {
        username: "_SYSADMIN",
        password: "123456",
        // grant_type: "password",
        // scope: "scope_tbyf",
        depaId: "",

        remember: []
      },
      // 表单印证规则
      loginFormRules: {
        username: [
          { required: true, message: "请输入登录用户名", trigger: "blur" },
          { min: 3, max: 12, message: "长度在 3 到 12 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入登录密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      },
      loading: false,
      redirect: undefined,
      otherQuery: {}
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  methods: {
    // 点击按钮表单重置
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields()
    },
    // 点击登录按钮
    login() {
      this.$refs.loginFormRef.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store
            .dispatch("user/login", this.loginForm)
            .then(() => {
              this.$router.push({
                path: this.redirect || "/",
                query: this.otherQuery
              })
              this.loading = false
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log("登入异常!!")
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }
}
</script>
<style lang="scss" scoped>
.login_container {
  background: url(../assets/images/login_bg.png) no-repeat;
  width: 100%;
  height: 100%;
  background-size: cover;
  position: relative;
  .login-logo {
    position: absolute;
    text-align: center;
    top: 6%;
    left: 50%;
    transform: translateX(-50%);
    font-size: 40px;
    color: #fff;
    letter-spacing: 2px;
    font-weight: bold;
  }
  .login_box {
    width: 73%;
    height: 60%;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, 0.05);
    padding: 4%;
    border-color: rgba(0, 0, 0, 0.05);
    background: hsla(0, 0%, 97.3%, 0.8);
    border-radius: 10px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .login-left {
      float: left;
      width: 50%;
      height: 100%;
      .el-image {
        width: 90%;
        height: 90%;
        overflow: hidden;
        display: flex;
        justify-content: center;
        align-items: center;
        img {
          display: inline-block;
          width: 100%;
        }
      }
    }
    .login-right {
      float: right;
      width: 50%;
      height: 100%;
      box-sizing: border-box;
      overflow: hidden;
      .login-main {
        // background: #fff;
        width: 90%;
        // box-shadow: 4px 4px 40px rgba(0,0,0,.07);
        border-radius: 8px;
        margin: 5px auto;
        height: 100%;
        padding: 0 12% 1%;
        display: flex;
        justify-content: center;
        align-items: center;
      }
    }
  }
}
.login_form {
  // padding: 0 12% 1%;
  padding: 50px 30px;
  background: #fff;
  width: 100%;
  box-sizing: border-box;
  .el-form-item {
    ::v-deep .el-form-item__content {
      line-height: 28px;
      .el-input__inner {
        height: 28px;
        line-height: 28px;
        padding: 0 15px 0 30px;
        margin-bottom: 0;
      }
      .el-input__prefix {
        height: auto;
      }
      .el-input__icon {
        line-height: 28px !important;
      }
    }
  }
}
.btns {
  .el-button {
    width: 100%;
  }
}
</style>
